package org.jeecg.modules.register.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.register.entity.AgencyLegal;
import org.jeecg.modules.register.entity.AgencyLegalCheckLog;
import org.jeecg.modules.register.entity.SubjectLegal;
import org.jeecg.modules.register.mapper.AgencyLegalCheckLogMapper;
import org.jeecg.modules.register.mapper.AgencyLegalMapper;
import org.jeecg.modules.register.service.IAgencyLegalCheckLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: agency_legal_check_log
 * @Author: jeecg-boot
 * @Date:   2024-12-22
 * @Version: V1.0
 */
@Service
public class AgencyLegalCheckLogServiceImpl extends ServiceImpl<AgencyLegalCheckLogMapper, AgencyLegalCheckLog> implements IAgencyLegalCheckLogService {

    @Resource
    public AgencyLegalMapper agencyLegalMapper;

    @Resource
    public AgencyLegalCheckLogMapper agencyLegalCheckLogMapper;

    @Override
    public IPage<AgencyLegalCheckLog> selChecklist(Page<AgencyLegalCheckLog> page, AgencyLegalCheckLog agencyLegalCheckLog) {
        return page.setRecords(agencyLegalCheckLogMapper.selCheckLoglist(page, agencyLegalCheckLog));
    }

    @Override
    public Result check(AgencyLegalCheckLog agencyLegalCheckLog) {

        if(null == agencyLegalCheckLog.getId()){
            return Result.error("ID不能为空！");
        }
        //0 待审核 1 起效 2 失效
        if(null == agencyLegalCheckLog.getState()){
            return Result.error("审核结果不能为空");
        }
        QueryWrapper<AgencyLegal> queryWrapper = new QueryWrapper<AgencyLegal>();
        queryWrapper.eq("legal_code", agencyLegalCheckLog.getLegalCode());
        AgencyLegal agencyLegal = agencyLegalMapper.selectOne(queryWrapper);

        if(null == agencyLegal){
            return Result.error("代理机构信息不存在，请先注册");
        }

        agencyLegalCheckLog.setCheckTime(new Date());
        if("1".equals(agencyLegalCheckLog.getState())){
            agencyLegal.setState("2");
            //更新用户表的的 注册状态为2
            agencyLegalMapper.updateSysUserRegistState(agencyLegal);

        }else{
            agencyLegal.setState("3");
        }
            agencyLegalCheckLogMapper.updateById(agencyLegalCheckLog);
            agencyLegalMapper.updateById(agencyLegal);

        return Result.OK("审核成功！");
    }
}
